<template>
	<el-card class="form-container">
		<el-form :model="dataForm" ref="dataForm" :rules="dataRule" @keyup.enter.native="dataFormSubmitHandle()" label-width="90px">
			<el-form-item label="类目编号" prop="categoryCode">
				<el-cascader-panel 
					:options="options"
					:props="setting"
					v-model="dataForm.categoryCode"
				>
				</el-cascader-panel>
			</el-form-item>
			<el-form-item>
				<el-button @click="cancel">取消</el-button>
				<el-button type="primary" @click="submit('dataForm')">确定</el-button>
			</el-form-item>
		</el-form>
	</el-card>
</template>

<script>
	import { addRelationBrandCategory} from '@/api/mall/goodsBrandCategory'
	import { findCategoryTree } from '@/api/mall/goodsCategory'
	export default {
		name: 'add-goodsBrandCategory',
		data() {
			return {
				dataForm: {
					brandCode: '',
					categoryCode: '',
				},
				dataRule: {
					categoryCode: [{
							required: true,
							message: '请输入类目编号',
							trigger: 'blur'
						},
					],
				},
				options:[],
				setting: {
					value: "code",
					label: "categoryName",
					children: "children",
					emitPath: false
				},
				
			}
		},
		created() {
			this.dataForm.brandCode = this.$route.query.brandCode
			this.initCategoryTree()
		},
		methods: {
			initCategoryTree(){
				findCategoryTree().then(res =>{
					this.options = this.getTreeData(res.data.responseData)
					
				})
			},
			getTreeData(data){
				for(var i=0;i<data.length;i++){
					if(data[i].children.length<1){
						data[i].children=undefined;
					}else {
						this.getTreeData(data[i].children);
					}
				}
				return data;
			},
			cancel() {
				this.$router.go(-1)
			},
			submit() {
				const set = this.$refs;
				set['dataForm'].validate((valid) => {
					if (!valid) {
						return false
					}
					addRelationBrandCategory(this.dataForm).then(() => {
						this.$notify({
							title: '添加成功',
							message: '添加成功',
							type: 'success',
							duration: 2000,
							onClose: this.$router.go(-1)
						})
					}).catch(err =>{
						this.$message.error(err.data.responseMessage)
					})
				})
			}
		}
	}
</script>

<style>
.el-cascader-menu__wrap{
	min-height: 400px;
}	
</style>
